<?php
/**
 * Модель узла.
 * 
 *
 * @package default
 * @author Alexander Sibiryakov
 **/
class AggregatedModel_Node extends RL_Model
{
	
	var $rbac_default_roles = array(
		array(
			'name'  => 'administrator',
			'title' => 'Администратор',
			'level' => 100,
		),
		array(
			'name'  => 'moderator',
			'title' => 'Модератор',
			'level' => 70,
		),
		array(
			'name'  => 'member',
			'title' => 'Участник сообщества',
			'level' => 50,
		),
		array(
			'name'  => 'registered',
			'title' => 'Зарегистрированный пользователь',
			'level' => 30,
		),
		array(
			'name'  => 'anonymous',
			'title' => 'Аноним',
			'level' => 1,
		),
	);
	
	var $rbac_default_policy = array(
		'administrator' => array(
			'comment_create' => 1,
			'comment_read' => 1,
			'comment_edit' => 1,
			'comment_delete' => 1,

			'community_create' => 1,
			'community_read' => 1,
			'community_edit' => 1,
			'community_delete' => 1,

			'item_create' => 1,
			'item_read' => 1,
			'item_edit' => 1,
			'item_delete' => 1,
// 			'item_approve' => 1,
// 			'item_freeze' => 1,

			'tdi_create' => 1,
			'tdi_read' => 1,
			'tdi_edit' => 1,
			'tdi_delete' => 1,
		),

		'moderator' => array(
			'comment_create' => 1,
			'comment_read' => 1,
			'comment_edit' => 1,
			'comment_delete' => 1,

			'community_create' => 1,
			'community_read' => 1,
			'community_edit' => 1,
			'community_delete' => 1,

			'item_create' => 1,
			'item_read' => 1,
			'item_edit' => 1,
			'item_delete' => 1,
// 			'item_approve' => 1,
// 			'item_freeze' => 1,

			'tdi_create' => 1,
			'tdi_read' => 1,
			'tdi_edit' => 1,
			'tdi_delete' => 1,
		),

		'registered' => array(
			'comment_create' => 1,
			'comment_read' => 1,
//		'comment_edit' => 1,
//		'comment_delete' => 1,

			'community_create' => 1,
			'community_read' => 1,
//		'community_edit' => 1,
//		'community_delete' => 1,

			'item_create' => 1,
			'item_read' => 1,
//		'item_edit' => 1,
//		'item_delete' => 1,

			'comment_create' => 1,
			'comment_read' => 1,
//		'comment_edit' => 1,
//		'comment_delte' => 1,

//		'tdi_create' => 1,
			'tdi_read' => 1,
//		'tdi_edit' => 1,
//		'tdi_delete' => 1,
		),

		'member' => array(
			'comment_create' => 1,
			'comment_read' => 1,
//		'comment_edit' => 1,
//		'comment_delete' => 1,

//		'community_create' => 1,
			'community_read' => 1,
//		'community_edit' => 1,
//		'community_delete' => 1,

			'item_create' => 1,
			'item_read' => 1,
//		'item_edit' => 1,
//		'item_delete' => 1,

//		'tdi_create' => 1,
			'tdi_read' => 1,
//		'tdi_edit' => 1,
//		'tdi_delete' => 1,
		),

		'anonymous' => array(
//		'comment_create' => 1,
			'comment_read' => 1,
//		'comment_edit' => 1,
//		'comment_delete' => 1,

//		'community_create' => 1,
			'community_read' => 1,
//		'community_edit' => 1,
//		'community_delete' => 1,

//		'item_create' => 1,
			'item_read' => 1,
//		'item_edit' => 1,
//		'item_delete' => 1,

//		'tdi_create' => 1,
//		'tdi_read' => 1,
//		'tdi_edit' => 1,
//		'tdi_delete' => 1,
		),
	);
	
	
	public function createNode() {
		$this->ctx->db->begin();
		$node = $this->store->create('fnode', array('title' => 'Main node', ));
		$res = $node->save();
		$res = $node->inSense("node");
		$res = $node->save();
		
		$this->acl_ass->CreateNode( $node->id );
		
		// создаем роли
		$roles = array();
		foreach ($this->rbac_default_roles as $role_data) {
			$name = $role_data['name'];
			$roles[$name] = $this->addRole( $role_data );
		}

		// создаем acl для ролей
		foreach ($this->rbac_default_policy as $role_name => $acl) {
			$role_oid = $roles[$role_name];
			$this->acl_ass->setAcl($node->id, $acl, $role_oid);
		}
/*
		// создаем администратора
		$admin = array(
			'nick' => '*mommy*',
			'openid_url' => '',
			'fullname' => 'Admin Adminovich',
			'bdate' => '',
			'login' => 'admin',
			'password' => md5('admin'),
		);
		
		$this->ctx->mm->getModel("User", "m")->addNewUser($admin, 'administrator'); // надо вызывать метод модели
		*/
		$this->ctx->db->end();
		
	}
	
	public function addRole( $role_data ) {
		// role name ('moderator', 'administrator', 'anonymous' & etc.)
		$role = $this->store->create('frole', $role_data);

		$res = $role->save();
		$res = $role->inSense("role");
		$res = $role->save();
		
		// тут еще добавление в субъекты
		$this->acl_ass->addRoleIntoNode($role->id, $role_data['name']);
		return $role->id;
	}
	
} // END class 
?>
